package cn.caiyj.algorithm.sort;

import cn.caiyj.algorithm.util.BaseUtil;
import com.alibaba.fastjson.JSON;

/**
 * @Description:希尔排序：
 * @author:CAIYJ
 * @Date:2018/10/24 22:02
 */
public class Shell {

    public static void sort(Comparable[] a, boolean print) {
        int n = a.length;
        int h = 1;
        while (h < n / 3) {
            h = 3 * h + 1;
        }
        while (h >= 1) {
            //将数组变为h有序
            for (int i = h; i < n; i++) {
                for (int j = i; j >= h && BaseUtil.less(a[j], a[j - h]); j -= h) {
                    if (print) {
                        System.out.println(JSON.toJSONString(a));
                    }
                    BaseUtil.exch(a, j, j - h);
                }
            }
            h = h / 3;
        }
    }
}
